﻿using EversysApi.DataObject;
using EversysApi.Services;
using Newtonsoft.Json;
using System;
using System.Linq;
using System.Text;
using System.Threading;

namespace DuBaiCoffee2.Model
{
    public class CoffeeErversysMsg
    {
        private static CoffeeErversysMsg instance;
        private CoffeeErversysMsg() { }

        public static CoffeeErversysMsg Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new CoffeeErversysMsg();
                }
                return instance;
            }
        }
        public SerialService serialService;
        public ApiFunctions apiFunctions;
        public bool IsCMconnected = false;
        public bool iscoffeeready = true;
        public bool isWLMsg = true;
        public bool IsReadyMilkL = true;
        public bool IsReadyMilkR = true;
        /// <summary>
        /// 咖啡机状态
        /// </summary>
        public void Makelist()
        {
            try
            {
                if (IsCMconnected == false)
                {

                    serialService = new SerialService(new NetSerialPort());
                    apiFunctions = new ApiFunctions(serialService);
                    serialService.Disconnect();
                    if (serialService.AutoConnect())
                    {

                        Thread thread = new Thread(IsMakingTrue);
                        thread.IsBackground = true;
                        thread.Start();
                        WuLiaoCoffeeSel.Instance.CoffeeWarn = true;
                        ClientList clientList = new ClientList();
                        clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                        clientList.code = 2;
                        SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                    }
                    else
                    {
                        serialService.Disconnect();
                        IsCMconnected = false;
                        Thread.Sleep(100);
                        Makelist();
                    }
                }
                else
                {
                    serialService.Disconnect();
                    IsCMconnected = false;
                    Thread.Sleep(100);
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        public void IsMakingTrue()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        if (iscoffeeready)
                        {

                            ApiStatus apiStatus;
                            ApiInfoMessages apiInfo;
                            apiFunctions.GetStatus(out apiStatus);
                            if (apiStatus != null)
                            {
                                //IsReadyCoffeeL = apiStatus.SteamL.status == EversysApi.Defines.ModuleStatus_t.Ready_e;
                                //IsReadyCoffeeR = apiStatus.SteamR.status == EversysApi.Defines.ModuleStatus_t.Ready_e;
                                if (MainWindow.isMake)
                                {
                                    bool iscoffee = true;
                                    bool iscoffeeR = true;
                                    apiFunctions.GetInfoMessages(out apiInfo);
                                    if (apiInfo != null)
                                    {
                                        if (apiInfo.Warnings.Count() > 0)
                                        {
                                            iscoffee = apiInfo.Warnings.Any(a => a == EversysApi.Defines.Warning_t.WarnCoffeeHopperLevelLeft_e) ? false : true;
                                            iscoffeeR = apiInfo.Warnings.Any(a => a == EversysApi.Defines.Warning_t.WarnCoffeeHopperLevelRight_e) ? false : true;
                                        }
                                        else
                                        {
                                           iscoffee= true;
                                           iscoffeeR = true;
                                        }
                                    }
                                    else
                                    {
                                        iscoffee = true;
                                        iscoffeeR = true;
                                    }
                                    //bool iscoffee = apiStatus.CoffeeMilkL.status != EversysApi.Defines.ModuleStatus_t.OnlyMilkUnitReady && apiStatus.CoffeeMilkL.status != EversysApi.Defines.ModuleStatus_t.notReady_e ? true : false;
                                    if (iscoffee != WuLiaoCoffeeSel.Instance.CoffeeBean2_Sel_Bol)
                                    {
                                        //CoffeeMatters.Instance.CoffeMatter11 = iscoffee;
                                        WuLiaoCoffeeSel.Instance.CoffeeBean2_Sel_Bol = iscoffee;
                                        isWLMsg = true;
                                    }
                                    //bool iscoffeeR = apiStatus.CoffeeMilkR.status != EversysApi.Defines.ModuleStatus_t.OnlyMilkUnitReady && apiStatus.CoffeeMilkR.status != EversysApi.Defines.ModuleStatus_t.notReady_e ? true : false;
                                    if (iscoffeeR != WuLiaoCoffeeSel.Instance.CoffeeBean_Sel_Bol)
                                    {
                                        //CoffeeMatters.Instance.CoffeMatter11 = iscoffee;
                                        WuLiaoCoffeeSel.Instance.CoffeeBean_Sel_Bol = iscoffeeR;
                                        isWLMsg = true;
                                    }
                                }
                                IsReadyMilkL = apiStatus.CoffeeMilkL.status == EversysApi.Defines.ModuleStatus_t.Ready_e || apiStatus.CoffeeMilkL.action == EversysApi.Defines.ModuleAction_t.ActionEnding_e;
                                IsReadyMilkR = apiStatus.CoffeeMilkL.action == EversysApi.Defines.ModuleAction_t.ActionIdle_e;
                                // Console.WriteLine(apiStatus.CoffeeMilkR.status + "R");
                                Console.WriteLine(IsReadyMilkL + " L");
                                Console.WriteLine(WuLiaoCoffeeSel.Instance.CoffeeBean_Sel_Bol + " L2");
                                Console.WriteLine(WuLiaoCoffeeSel.Instance.CoffeeBean2_Sel_Bol + " R");
                                ////   Console.WriteLine(apiStatus.CoffeeMilkR.action + "R");
                                //Console.WriteLine(apiStatus.CoffeeMilkL.process + "L");
                                ////    Console.WriteLine(apiStatus.CoffeeMilkR.process + "R");
                                //Console.WriteLine(IsReadyMilkL);
                                //  IsReadyMilkR = apiStatus.CoffeeMilkR.status == EversysApi.Defines.ModuleStatus_t.Ready_e;
                            }
                            if (isWLMsg)
                            {
                                TestDelegateInvoke testDelegate = new TestDelegateInvoke(DelegateInvokeMethod);
                                testDelegate.BeginInvoke(null, null);
                                ClientList clientList = new ClientList();
                                clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                                clientList.code = 2;
                                SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                                isWLMsg = false;
                            }
                        }
                        Thread.Sleep(1000);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        public delegate void TestDelegateInvoke();

        private void DelegateInvokeMethod()
        {
            try
            {
                EmailShow emailShow = new EmailShow();
                emailShow.content = MainWindow.EmailError();
                var saves1 = JsonConvert.SerializeObject(emailShow);
                Console.WriteLine(emailShow.content);
                var dd1 = HttpMsg.Instance.sendEmailMsg(saves1);
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
